package com.atguigu.fruit.servlets;

import com.atguigu.fruit.dao.FruitDAO;
import com.atguigu.fruit.dao.impl.FruitDAOImpl;
import com.atguigu.fruit.pojo.Fruit;
import com.atguigu.myssm.myspringmvc.ViewBaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * liujianlong
 * 2022-01-25 15:46
 */
@WebServlet("/update.do")
public class UpdateServlet extends ViewBaseServlet {
    private FruitDAO fruitDAO=new FruitDAOImpl();
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
        //1设置编码
        request.setCharacterEncoding("UTF-8");

        //2获取参数
        String fidStr=request.getParameter("fid");
        Integer fid= Integer.parseInt(fidStr);
        String fname = request.getParameter("fname");
        String priceStr=request.getParameter("price");
        int price=Integer.parseInt(priceStr);
        String fcountStr=request.getParameter("fcount");
        Integer fcount= Integer.parseInt(fcountStr);
        String remark=request.getParameter("remark");

        //3执行更新
        fruitDAO.updateFruit(new Fruit(fid,fname,price,fcount,remark));

        //4资源跳转
        //super.processTemplate("index",request,response);
        //request.getRequestDispatcher("index.html").farward(request,response);
        //此处需要重定向，目的是重新给IndexServlet发请求，重新获取fruitList,然后覆盖到session中，
        //这样index.html页面显示的session中的数据才是最新的
        response.sendRedirect("index");


    }
}
// java.lang.NumberFormatException: For input string: ""